package com.yu.mail.starter.configs;

import com.yu.mail.starter.properties.MailProperties;
import com.yu.mail.starter.service.MailService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 邮件自动配置类
 * @author yuhh
 * @date 2021-05-24 17:30
 **/
@Configuration
@ConditionalOnClass(MailService.class)
@EnableConfigurationProperties(MailProperties.class)
public class MailAutoConfig {

    @Resource
    private MailProperties properties;

    @Bean
    @ConditionalOnMissingBean()
    public MailSender mailSender(){
        return new MailSender(properties);
    }

    @Bean
    @ConditionalOnMissingBean()
    public MailService mailService() {
        return new MailService(properties, this.mailSender());
    }
}
